package com.example.echoewish.ui.my_activity.chat_activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.echoewish.econstant.Constant;
import com.example.echoewish.econstant.UserInfo;
import com.example.echoewish.pojo.Chat;
import com.example.echoewish.pojo.ChatEntity;
import com.example.echoewish.pojo.MyConcern;
import com.example.echoewish.repository.ChatMessageRepository;
import com.example.echoewish.util.EchoWishApplication;

import java.util.LinkedList;
import java.util.List;

//先实现发送功能
public class ChatViewModel extends ViewModel {
    public MutableLiveData<List<ChatEntity>> chatEntityMutableLiveData;
    public MutableLiveData<List<Chat>> chatsMutableLiveData;
    public MutableLiveData<String> replyMutableLiveData;
    public MutableLiveData<Chat> otherReplyMutableLiveData;
    public MutableLiveData<MyConcern> otherMutableLiveData;
    public MutableLiveData<Boolean> isConnect;
    public ChatMessageRepository chatMessageRepository;
    ChatMessageBroadCastReceiver chatMessageBroadCastReceiver=null;

    public ChatViewModel()
    {
        chatEntityMutableLiveData=new MutableLiveData<>();
        chatsMutableLiveData=new MutableLiveData<>();
        otherMutableLiveData =new MutableLiveData<>();
        replyMutableLiveData=new MutableLiveData<>();
        otherReplyMutableLiveData=new MutableLiveData<>();
        chatMessageBroadCastReceiver=new ChatMessageBroadCastReceiver();
        isConnect=new MutableLiveData<>();
        chatsMutableLiveData.setValue(new LinkedList<>());
    }

    public void initChatMessageRepository()
    {
        if(otherMutableLiveData.getValue().friId!=-1)
        {
            chatMessageRepository=new ChatMessageRepository(UserInfo.getInstance().UserId, otherMutableLiveData.getValue().friId);
            chatEntityMutableLiveData.postValue(chatMessageRepository.getChatMessage(UserInfo.getInstance().UserId,otherMutableLiveData.getValue().friId));
        }
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction(Constant.ACTION_RECEIVE_MESSAGE);
        EchoWishApplication.getContext().registerReceiver(chatMessageBroadCastReceiver,intentFilter);
    }

    public void sendMessage()
    {
        chatMessageRepository.sendMessage(replyMutableLiveData.getValue());
    }

    public void deleteChatMessage()
    {
        chatMessageRepository.deleteChatMessage();
        chatsMutableLiveData.postValue(new LinkedList<>());
    }

    public void closeConnect()
    {
        EchoWishApplication.getContext().unregisterReceiver(chatMessageBroadCastReceiver);
        chatMessageRepository.closeConnect();
    }

    public void storeInDatabase(Chat chat)
    {
        chatMessageRepository.storeInDatabase(new ChatEntity(chat));
    }

    private class ChatMessageBroadCastReceiver extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent) {
            Chat chat=new Chat();
            chat.content=intent.getStringExtra(Constant.KEY_RECEIVED_DATA_CONTENT);
            chat.time=intent.getStringExtra(Constant.KEY_RECEIVED_DATA_TIME);
            otherReplyMutableLiveData.postValue(chat);
        }
    }
}
